package com.zerox.controller;


import com.alibaba.fastjson.JSON;
import com.zerox.entity.SysDept;
import com.zerox.entity.SysUser;
import com.zerox.service.SysDeptService;
import com.zerox.service.SysUserService;
import com.zerox.uilt.UserListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "user", produces = {"application/json;charset=utf-8"})
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("touser")
    public String list() {
        return "user/user";
    }


    @GetMapping("tolist")
    public String list(HttpSession session) {
//        List<SysUser> sysUsers = sysUserService.list();
//        session.setAttribute("sysuser",sysUsers);
        return "list";
    }

    @GetMapping("toadd")
    public String add() {
        return "/user/add";
    }


    @GetMapping("/toedit/{userId}")
    public String edit(@PathVariable Integer userId, HttpServletRequest request) {
        SysUser sysUser = sysUserService.userById(userId);
        request.setAttribute("SysUser", sysUser);
        return "user/edit";
    }

    @PostMapping(value = "add", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String add2(SysUser sysUser) {

        int row = sysUserService.add(sysUser);

//        sysUserService.addrelevancy()
        return row > 0 ? "1" : "0";
    }

    //删除
    @GetMapping("/delete/{userId}")
    @ResponseBody
    public String delete(@PathVariable("userId") Integer id) {
        int row = sysUserService.delete(id);
        return row > 0 ? "1" : "0";
    }


    @GetMapping(value = "/list")
    @ResponseBody
    public String list(Integer page, Integer limit, @RequestParam(value = "userName", required = false) String userName, @RequestParam(value = "roleId", required = false) Integer roleId) {

        List<SysUser> userList = sysUserService.listUser(page, limit, userName, roleId);
        System.out.println(userList);
        int count = sysUserService.count(userName, roleId);
        UserListVO userListVO = new UserListVO(0, count, userList, "");
        System.out.println(userListVO.getData());
//        System.out.println(userListVO);
        return JSON.toJSONString(userListVO);
    }


    @PostMapping("update")
    @ResponseBody
    public String update(SysUser sysUser, HttpServletRequest request) {
        int row = sysUserService.update(sysUser);

        return row > 0 ? "1" : "0";
    }


    @GetMapping(value = "tree",produces = {"application/json;charset =utf-8"})
    @ResponseBody
    public String buildDeptTree(@RequestParam List<SysDept> depts) {
        List<SysDept> list = sysDeptService.buildDeptTree(depts);
        return JSON.toJSONString(list);
    }

}
